package com.chixing.server.listener;


import com.alibaba.fastjson.JSON;
import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.RequestMessage;
import com.chixing.service.EmailService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@RabbitListener(queues = "RemindFirstToReturnTheCar")
public class ReceiverDelay {


    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private EmailService emailService;
    @Autowired
    private CustomerMapper customerMapper;
    @RabbitHandler
    public void process(String context)  {
        System.out.println("ReceiverDelay : " + context);

        RequestMessage mqTask =  JSON.parseObject(context,RequestMessage.class) ;
        if (Objects.equals( mqTask.getType(), "2" )) {
            String destination = "/topic/" +mqTask.getRoom();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Customer customer = customerMapper.selectByPrimaryKey(Integer.valueOf(mqTask.getRoom()));
            messagingTemplate.convertAndSend(destination, mqTask);
            emailService.sendSimpleMail(customer.getCustEmail(),"小橘租车通知",mqTask.getContent());

        }
    }
}
